/**
 * 
 */
package org.dragonfire.editor.z80.hiliteRules;

import org.eclipse.jface.text.rules.ICharacterScanner;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.Token;

/**
 * @author Darth Android
 * @date Oct 2, 2007
 */
public class LabelRule implements IRule {

	public static final char LABEL_END_CHAR = ':';
	private IToken token;

	public LabelRule(IToken token) {
		this.token = token;
	}

	public IToken evaluate(ICharacterScanner scanner) {
		if (scanner.getColumn() != 0)
			return Token.UNDEFINED;
		return evaluate(scanner, false);
	}

	public IToken evaluate(ICharacterScanner scanner, boolean seenText) {
		char c = (char) scanner.read();
		if (c == ICharacterScanner.EOF) {
			scanner.unread();
			return Token.UNDEFINED;
		}
		if (c == LABEL_END_CHAR)
			return token;
		IToken t = Token.UNDEFINED;
		if (Character.isWhitespace(c) && !seenText)
			t = evaluate(scanner, false);
		if ("_.abcdefghijklmnopqrstuvwxyz0123456789@".contains(Character.toLowerCase(c) + ""))
			t = evaluate(scanner, true);
		if (t == Token.UNDEFINED)
			scanner.unread();
		return t;
	}
}
